package com.example.questionexam.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.questionexam.common.BaseResponse;
import com.example.questionexam.common.DeleteRequest;
import com.example.questionexam.common.ErrorCode;
import com.example.questionexam.common.ResultUtils;
import com.example.questionexam.exception.BusinessException;
import com.example.questionexam.exception.ThrowUtils;
import com.example.questionexam.model.dto.answer.AnswerAddRequest;
import com.example.questionexam.model.dto.answer.AnswerUpdateRequest;
import com.example.questionexam.model.entity.Answer;
import com.example.questionexam.service.AnswerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @author Maobohe
 * @createData 2024/5/28 10:35
 */
@RestController
@RequestMapping("/answer")
@Slf4j
public class AnswerController {

    @Resource
    private AnswerService answerService;

    /**
     * 创建题目
     *
     * @param answerAddRequest
     * @param request
     * @return
     */
    @PostMapping("/add")
    public BaseResponse<Long> addAnswer(@RequestBody AnswerAddRequest answerAddRequest, HttpServletRequest request) {
        if (answerAddRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Answer answer = new Answer();
        BeanUtils.copyProperties(answerAddRequest, answer);
        // 校验
        answerService.validQuestion(answer, true);
        boolean result = answerService.save(answer);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(answer.getId());
    }

    /**
     * 删除题目
     *
     * @param deleteRequest
     * @param request
     * @return
     */
    @PostMapping("/delete")
    public BaseResponse<Boolean> deleteAnswer(@RequestBody DeleteRequest deleteRequest, HttpServletRequest request) {
        if (deleteRequest == null || deleteRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        QueryWrapper<Answer> queryWrapper = new QueryWrapper<>();
        Long questionId = deleteRequest.getId();
        queryWrapper.eq("questionId", questionId);
        boolean remove = answerService.remove(queryWrapper);
        return ResultUtils.success(remove);
    }

    /**
     * 更新题目
     *
     * @param answerUpdateRequest
     * @param request
     * @return
     */
    @PostMapping("/update")
    public BaseResponse<Boolean> updateAnswer(@RequestBody AnswerUpdateRequest answerUpdateRequest,
                                            HttpServletRequest request) {
        if (answerUpdateRequest == null || answerUpdateRequest.getQuestionId() == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Answer answer = new Answer();
        BeanUtils.copyProperties(answerUpdateRequest, answer);
        // 校验
        answerService.validQuestion(answer, false);
        QueryWrapper<Answer> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("questionId", answer.getQuestionId());
        Answer oldAnswer = answerService.getOne(queryWrapper);
        ThrowUtils.throwIf(oldAnswer == null, ErrorCode.NOT_FOUND_ERROR);
        boolean result = answerService.update(answer, queryWrapper);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(true);
    }
}
